package com.ruoyi.web.controller.ycdj;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.THonor;
import com.ruoyi.system.service.THonorService;
import com.ruoyi.system.service.TOrganizationService;
import com.ruoyi.system.service.TUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("honor")
@Api(tags = "荣誉墙")
public class HonorController extends BaseController {

    @Autowired
    public THonorService tHonorService;

    @Autowired
    public TUserService tUserService;

    @Autowired
    public TOrganizationService tOrganizationService;

    //列表
    @ApiOperation("列表")
    @GetMapping("getlist")
    public AjaxResult getlist(@RequestParam(value="pageNum",defaultValue = "")Integer pageNum,
                              @RequestParam(value="pageSize",defaultValue = "")Integer pageSize,
                              @RequestParam(value="honorclass",defaultValue = "")Integer honorclass,
                              @RequestParam(value="userororg",defaultValue = "")Integer userororg){
        QueryWrapper<THonor>tHonorQueryWrapper=new QueryWrapper<>();
        tHonorQueryWrapper.eq("status",1);
        if(honorclass!=null) {
            tHonorQueryWrapper.eq("honorclass", honorclass);
        }
        if(userororg!=null){
            tHonorQueryWrapper.eq("userororg",userororg);
        }
        if(pageNum!=null){
            PageHelper.startPage(pageNum,pageSize);
            List<THonor>tHonorList=tHonorService.list(tHonorQueryWrapper);
            if(!tHonorList.isEmpty()){
                for(THonor tHonor:tHonorList){
                    tHonor.setOrgname(tOrganizationService.getById(tHonor.getUserid()).getName());
                }
            }
            PageInfo<THonor>tHonorPageInfo=new PageInfo<>(tHonorList);
            return AjaxResult.success(tHonorPageInfo);
        }else{
            List<THonor>tHonorList=tHonorService.list(tHonorQueryWrapper);
            if(!tHonorList.isEmpty()){
                for(THonor tHonor:tHonorList){
                    if(tHonor.getUserororg()==0){
                        tHonor.setOrgname(tUserService.getById(tHonor.getUserid()).getUsername());
                    }else{
                        tHonor.setOrgname(tOrganizationService.getById(tHonor.getUserid()).getName());
                    }
                }
            }
            return AjaxResult.success(tHonorList);
        }
    }

    //新增
    @ApiOperation("新增")
    @PostMapping("add")
    public AjaxResult add(@RequestBody THonor tHonor){
        tHonor.setCreatetime(new Date());
        return toAjax(tHonorService.save(tHonor));
    }

    @ApiOperation("编辑")
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody THonor tHonor){
        return toAjax(tHonorService.saveOrUpdate(tHonor));
    }

    @ApiOperation("删除")
    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        THonor tHonor=tHonorService.getById(id);
        tHonor.setStatus(0);
        return toAjax(tHonorService.saveOrUpdate(tHonor));
    }

}
